package team.se.tms.controller.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.constant.SessionConstant;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.po.TaskInfo;
import team.se.tms.po.UserInfo;
import team.se.tms.service.AssignmentInfoService;
import team.se.tms.service.TaskInfoService;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.ResultUtil;

import javax.annotation.Resource;

/**
 * 任务信息控制层
 *
 * @author winkyle
 * @date 2018/11/25
 */
@Slf4j
@Controller
@RequestMapping(value="/task")
public class TaskInfoController extends BaseController{
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private AssignmentInfoService assignmentInfoService;
    /**
     * 访问我的任务详情界面
     *
     * @param taskInfo 任务信息
     * @return ModelAndView 页面模板
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/view_my_task_details")
    @CheckFormat
    public ModelAndView viewMyTaskDetails(TaskInfo taskInfo) throws Exception{
        log.debug("========================viewMyTaskDetails{}",taskInfo.toString());
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("taskInfo", taskInfo);
        modelAndView.addObject("assignmentFile",assignmentInfoService.getAssignmentFile(((UserInfo) Jurisdiction.getSession().getAttribute(SessionConstant.USER)).getUserId(),taskInfo.getTaskId()));
        modelAndView.setViewName("select_course/my_task_details");
        return modelAndView;
    }

    /**
     * 访问创建任务界面
     *
     * @return ModelAndView 页面模板
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/to_add_task")
    public ModelAndView toAddTask(@SpecialCheck("Pk")String teachCourseId){
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("type","add");
        modelAndView.addObject("teachCourseId",teachCourseId);
        modelAndView.setViewName("teach_course/create_modify_task");
        return modelAndView;
    }

    /**
     * 访问修改任务界面
     *
     * @param taskInfo 任务信息
     * @return ModelAndView 页面模板
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/to_edit_task")
    @CheckFormat
    public ModelAndView toEditTask(@SpecialCheck("Pk")String teachCourseId,TaskInfo taskInfo){
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("type","edit");
        modelAndView.addObject("teachCourseId",teachCourseId);
        modelAndView.addObject("taskInfo",taskInfo);
        modelAndView.setViewName("teach_course/create_modify_task");
        return modelAndView;
    }

    /**
     * 根据授课id获取任务详细信息
     * todo
     * @param teachCourseId 授课id
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/search_task_details")
    @ResponseBody
    public Object searchTaskDetails(@SpecialCheck("Pk")String teachCourseId, Page page) throws Exception {
        return page.getDataTabelJson(taskInfoService.listTaskDetails(teachCourseId, page));
    }

    /**
     * 根据授课id获取任务信息
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/search_task_infos")
    @ResponseBody
    public Object searchTaskInfos(@SpecialCheck("Pk")String teachCourseId, Page page) throws Exception {
        log.debug("===========================teachCourseId:{},page:{}",teachCourseId,page.toString());
        return page.getDataTabelJson(taskInfoService.listTaskInfos(teachCourseId, page));
    }

    /**
     * 创建任务
     *
     * @param taskInfo 任务信息
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/add_task_info")
    @ResponseBody
    @CheckFormat({"taskId"})
    public Object addTaskInfo(TaskInfo taskInfo) throws Exception {
        log.debug("addTaskInfo{}",taskInfo.toString());
        taskInfoService.saveTaskInfo(taskInfo);
        return ResultUtil.success("创建成功");
    }

    /**
     * 修改任务
     *
     * @param taskInfo 任务信息
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/6 by winkyle
     */
    @RequestMapping(value="/edit_task_info")
    @ResponseBody
    @CheckFormat
    public Object editTaskInfo(TaskInfo taskInfo) throws Exception {
        log.debug("editTaskInfo{}",taskInfo.toString());
        taskInfoService.modifyTaskInfo(taskInfo);
        return ResultUtil.success("修改成功");
    }

    @RequestMapping(value="/searchTaskDetails")
    @ResponseBody
    @CheckFormat
    public Object searchTaskDetails(String teachCourseId) throws Exception {
        System.out.println("7777777777777777"+teachCourseId);
//        taskInfoService.modifyTaskInfo(taskInfo);
        return ResultUtil.success("修改成功");
    }


}
